{% extends "base.html" %}

{% block title %}Posts - Flask Database Demo{% endblock %}

{% block content %}
<h2>Post Management</h2>

<div class="row">
    <div class="col-md-8">
        {% if posts %}
            {% for post in posts %}
                <div class="card">
                    <div class="card-header">
                        <h5>{{ post.title }}</h5>
                        <small class="text-muted">
                            By <a href="{{ url_for('user_posts', user_id=post.author.id) }}">{{ post.author.username }}</a> 
                            on {{ post.timestamp.strftime('%Y-%m-%d %H:%M') }}
                        </small>
                    </div>
                    <div class="card-body">
                        <p class="card-text">{{ post.body }}</p>
                    </div>
                    <div class="card-footer">
                        <a href="{{ url_for('edit_post', post_id=post.id) }}" class="btn btn-sm btn-outline-secondary">Edit</a>
                        <a href="{{ url_for('delete_post', post_id=post.id) }}" class="btn btn-sm btn-outline-danger" 
                           onclick="return confirm('Are you sure you want to delete this post?')">Delete</a>
                    </div>
                </div>
            {% endfor %}
            
            <!-- Pagination -->
            {% if pagination.pages > 1 %}
                <nav aria-label="Post pagination">
                    <ul class="pagination">
                        {% if pagination.has_prev %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('posts', page=pagination.prev_num) }}">Previous</a>
                            </li>
                        {% else %}
                            <li class="page-item disabled">
                                <span class="page-link">Previous</span>
                            </li>
                        {% endif %}
                        
                        {% for page_num in pagination.iter_pages() %}
                            {% if page_num %}
                                {% if page_num != pagination.page %}
                                    <li class="page-item">
                                        <a class="page-link" href="{{ url_for('posts', page=page_num) }}">{{ page_num }}</a>
                                    </li>
                                {% else %}
                                    <li class="page-item active">
                                        <span class="page-link">{{ page_num }}</span>
                                    </li>
                                {% endif %}
                            {% else %}
                                <li class="page-item disabled">
                                    <span class="page-link">…</span>
                                </li>
                            {% endif %}
                        {% endfor %}
                        
                        {% if pagination.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('posts', page=pagination.next_num) }}">Next</a>
                            </li>
                        {% else %}
                            <li class="page-item disabled">
                                <span class="page-link">Next</span>
                            </li>
                        {% endif %}
                    </ul>
                </nav>
            {% endif %}
        {% else %}
            <div class="alert alert-info">
                <p>No posts found in the database.</p>
                <a href="{{ url_for('create_post') }}" class="btn btn-primary">Create First Post</a>
            </div>
        {% endif %}
    </div>
    
    <div class="col-md-4">
        <div class="card">
            <div class="card-header">
                <h5>Post Statistics</h5>
            </div>
            <div class="card-body">
                <p><strong>Total Posts:</strong> {{ posts|length }}</p>
                <p><strong>Authors:</strong> {{ authors_count }}</p>
            </div>
        </div>
        
        <div class="card mt-3">
            <div class="card-header">
                <h5>About Post Management</h5>
            </div>
            <div class="card-body">
                <p>This section demonstrates:</p>
                <ul>
                    <li>Querying posts with pagination</li>
                    <li>Displaying post content and metadata</li>
                    <li>Navigating to author profiles</li>
                    <li>Editing and deleting posts</li>
                </ul>
            </div>
        </div>
    </div>
</div>

<div class="mt-3">
    <a href="{{ url_for('index') }}" class="btn btn-secondary">Back to Home</a>
    <a href="{{ url_for('create_post') }}" class="btn btn-primary">Create New Post</a>
</div>
{% endblock %}